package com.zzyl.intercept;

import cn.hutool.json.JSONUtil;

import com.zzyl.constant.CacheConstant;
import com.zzyl.constant.Constants;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.properties.JwtTokenManagerProperties;
import com.zzyl.utils.JwtUtil;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.utils.StringUtils;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.UserVo;
import io.jsonwebtoken.Claims;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;


@Component
public class UserTokenInterceptor implements HandlerInterceptor {
    @Autowired
    private JwtTokenManagerProperties jwtTokenManagerProperties;
    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    private AntPathMatcher antPathMatcher=new AntPathMatcher();
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if(!(handler instanceof HandlerMethod)){
            return true;
        }
        //获取token
        String token = request.getHeader(Constants.USER_TOKEN);
        //判断是否包含token
        if(StringUtils.isEmpty(token)){//如果为空
            throw new BaseException(BasicEnum.LOGIN_LOSE_EFFICACY);
        }
        //解析JWT
        Claims claims = JwtUtil.parseJWT(jwtTokenManagerProperties.getBase64EncodedSecretKey(), token);
        //判断是否含有共享数据
        if(ObjectUtil.isEmpty(claims)){
            throw new BaseException(BasicEnum.LOGIN_LOSE_EFFICACY);
        }

        //获取共享数据
        String userJSON = String.valueOf(claims.get("currentUser"));
        UserThreadLocal.setSubject(userJSON);
        return true;
        //把字符串转换为对象
/*        UserVo user = JSONUtil.toBean(userJSON, UserVo.class);
        //从redis获取数据
        String key = CacheConstant.PUBLIC_ACCESS_URLS + user.getId();
        String urlJson= redisTemplate.opsForValue().get(key);
        //判断当前访问路径是否属于有权限的路径
        List<String> urlList=null;
        if(!(StringUtil.isNullOrEmpty(urlJson))) {
            urlList=JSONUtil.toList(urlJson,String.class);
        }
        //获取当前访问路径
        String requestUrl=request.getMethod()+request.getRequestURI();
        //遍历集合进行匹配
        for (String url : urlList) {
            if(antPathMatcher.match(url,requestUrl)){
                //把数据用户存储到threadlocal中
                UserThreadLocal.setSubject(userJSON);
                return true;
            }
        }*/
            /*throw new BaseException(BasicEnum.SECURITY_ACCESSDENIED_FAIL);*/
        }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        UserThreadLocal.removeSubject();
    }
}
